/*
 *   Copyright 2012, Thomas Kerber
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package milk.skimmed;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * A wrapper to achieve simply what it says: forcing a class to be loaded.
 * @author Thomas Kerber
 * @version 1.0.0
 */
public class ForceClassLoader extends ClassLoader{
    
    /**
     * The loaders instance.
     */
    public static final ForceClassLoader INSTANCE = new ForceClassLoader(); 
    
    /**
     * Forcibly defines a class.
     * @param name The classname to define.
     * @param bytes The class bytes.
     * @param off The offset in the bytearray to start from.
     * @param len The length of the class in the bytes.
     * @return The defined and loaded class.
     */
    public Class<?> instanceForceDef(String name, byte[] bytes, int off,
            int len){
        return defineClass(name, bytes, off, len);
    }
    
    /**
     * Forcibly defines a class.
     * @param name The classname to define.
     * @param class_ The classfile to use.
     * @return The defined and loaded class.
     * @throws IOException
     */
    public Class<?> instanceForceLoad(String name, File class_)
            throws IOException{
        InputStream in = new FileInputStream(class_);
        byte[] save = new byte[1 << 20]; // 1 Mib save space by default.
        int off = 0;
        // Default read chunks: 1 Kib
        while(true){
            // Checks if there is enough space to add to buffer
            if(save.length < off + (1 << 10)){
                // Else expands array
                save = Arrays.copyOf(save, save.length + (1 << 20));
            }
            int am_added = in.read(save, off, 1 << 10);
            off += am_added == -1 ? 0 : am_added;
            if(am_added < (1 << 10))
                break;
        }
        in.close();
        return defineClass(name, save, 0, off);
    }
    
    /**
     * Forcibly defines a class.
     * @param name The classname to define.
     * @param class_ The classfile to use.
     * @return The defined and loaded class.
     * @throws IOException
     */
    public static Class<?> forceLoad(String name, File class_)
            throws IOException{
        return INSTANCE.instanceForceLoad(name, class_);
    }
    
    /**
     * Forcibly defines a class.
     * @param name The classname to define.
     * @param bytes The class bytes.
     * @param off The offset in the bytearray to start from.
     * @param len The length of the class in the bytes.
     * @return The defined and loaded class.
     */
    public static Class<?> forceDef(String name, byte[] bytes, int off,
            int len){
        return INSTANCE.instanceForceDef(name, bytes, off, len);
    }
    
}
